(function () {
    YUI.add("chat", function(Y) {
        var Chat = function(cfg) {
            Chat.superclass.constructor.apply(this, arguments);
        }
        Chat.NAME = "chat";
        Chat.ATTRS = {
        }
        Y.extend(Chat, Y.Base, {
            _last:undefined,
            _username:undefined,
            _connected:false,

            initializer:function(cfg) {
                Y.log(this + '.initializer() - initialized', 'debug', 'chat-yui3');
            },
            join:function(name) {
                /*        if (name == null || name.length == 0) {
                 alert('Please enter a username!');
                 return;
                 }

                 var loc = (new String(document.location).replace(/http:\/\/[^\/]*//*, '').replace(/\/examples\/.*$/, '')) +
                 "/cometd";
                 dojox.cometd.init(loc);
                 // For x-domain test change line above to:
                 // dojox.cometd.init("http://127.0.0.1:8080/cometd/cometd");
                 this._connected = true;

                 this._username = name;
                 dojo.addClass("join", "hidden");
                 dojo.removeClass("joined", "hidden");
                 dojo.byId("phrase").focus();

                 // subscribe and join
                 dojox.cometd.startBatch();
                 dojox.cometd.subscribe("/chat/demo", room, "_chat");
                 dojox.cometd.publish("/chat/demo", {
                 user: room._username,
                 join: true,
                 chat: room._username + " has joined"
                 });
                 dojox.cometd.endBatch();

                 // handle cometd failures while in the room
                 if (room._meta) {
                 dojo.unsubscribe(room._meta, null, null);
                 }
                 room._meta = dojo.subscribe("/cometd/meta", this, function(e) {
                 // console.debug(e);
                 if (e.action == "handshake") {
                 if (e.reestablish) {
                 if (e.successful) {
                 dojox.cometd.subscribe("/chat/demo", room, "_chat");
                 dojox.cometd.publish("/chat/demo", {
                 user: room._username,
                 join: true,
                 chat: room._username + " has re-joined"
                 });
                 }
                 room._chat({
                 data: {
                 join: true,
                 user: "SERVER",
                 chat: "handshake " + e.successful ? "Handshake OK" : "Failed"
                 }
                 });
                 }
                 }
                 else
                 if (e.action == "connect") {
                 if (e.successful && !this._connected) {
                 room._chat({
                 data: {
                 join: true,
                 user: "SERVER",
                 chat: "reconnected!"
                 }
                 });
                 }
                 if (!e.successful && this._connected) {
                 room._chat({
                 data: {
                 leave: true,
                 user: "SERVER",
                 chat: "disconnected!"
                 }
                 });
                 }
                 this._connected = e.successful;
                 }
                 });*/
            } ,
            leave:function() {
                /*if (!room._username) {
                 return;
                 }

                 if (room._meta) {
                 dojo.unsubscribe(room._meta);
                 }
                 room._meta = null;

                 dojox.cometd.startBatch();
                 dojox.cometd.unsubscribe("/chat/demo", room, "_chat");
                 dojox.cometd.publish("/chat/demo", {
                 user: room._username,
                 leave: true,
                 chat: room._username + " has left"
                 });
                 dojox.cometd.endBatch();

                 // switch the input form
                 dojo.removeClass("join", "hidden");
                 dojo.addClass("joined", "hidden");
                 dojo.byId("username").focus();
                 room._username = null;
                 dojox.cometd.disconnect();
                 dojo.byId('members').innerHTML = "";*/
            },
            chat:function() {
                /*        	if (!text || !text.length) {
                 return false;
                 }
                 var colons = text.indexOf("::");
                 if (colons > 0) {
                 dojox.cometd.publish("/service/privatechat", {
                 room: "/chat/demo", // This should be replaced by the room name
                 user: room._username,
                 chat: text.substring(colons + 2),
                 peer: text.substring(0, colons)
                 });
                 } else {
                 dojox.cometd.publish("/chat/demo", {
                 user: room._username,
                 chat: text
                 });
                 }*/
            }
        })
        Y.Chat = Chat;
    }, '3.0.0pr2')
}())